package com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.ContactService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.ContactDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.ContactListDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
 * 医保服务人员Service实现类
 * 
 * @author EX-LUORONG001
 * 
 */
@Service("contactService")
public class ContactServiceImpl extends BaseService implements ContactService {
	@Autowired
	private CommonDAO commonDAO;

	@Override
	public Map<String, Object> addContact(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			ContactDTO contactDTO = null;
			contactDTO = JSONObject
					.toJavaObject(transferJson, ContactDTO.class);
			contactDTO.setDepartment(contactDTO.getDepartmentNo());
			commonDAO.insert("healthcare.addContact", contactDTO);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.INSERT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> updateContact(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			ContactDTO contactDTO = null;
			contactDTO = JSONObject
					.toJavaObject(transferJson, ContactDTO.class);
			contactDTO.setDepartment(contactDTO.getDepartmentNo());
			commonDAO.update("healthcare.updateContact", contactDTO);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.UPDATE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> queryContactList(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map> result2 = null;
		try {
			String providerid = transferJson.getString("providerid");
			Map parm = new HashMap<String, String>();
			parm.put("providerId", providerid);
			result2 = commonDAO.query("healthcare.queryContactList", parm);
			// responsible = commonDAO.
			result.put(Constants.SUCCESS_RESULT_KEY, result2);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> queryContactDetail(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> result = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ContactDTO contactDTO = null;
			contactDTO = JSONObject
					.toJavaObject(transferJson, ContactDTO.class);
			result = (Map<String, Object>) commonDAO.get(
					"healthcare.queryContactDetail", contactDTO);

			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
			resultMap.put(Constants.SUCCESS_RESULT_KEY, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteContact(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, String> parms = new HashMap<String, String>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String ids = transferJson.getString("ids");
			StringBuffer sb = new StringBuffer();
			if (null != ids && !"".equals(ids)) {
				String[] id = ids.split(",");
				for (int i = 0; i < id.length; i++) {
					sb.append(id[i] + ",");
				}
				ids = sb.toString()
						.substring(0, sb.toString().lastIndexOf(','));
				parms.put("ids", ids);
				parms.put("providerids", transferJson.getString("providerids"));
				parms.put("updateby", transferJson.getString("updateby"));
				commonDAO.update("healthcare.deleteContact", parms);
				resultMap.put(Constants.STATE_KEY,
						Constants.SUCCESS_STATE_VALUE);
				resultMap.put(Constants.MSG_KEY, Constants.DELETE_SUCCESS);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> addInsurancePer(JSONArray transferJson,
			String userName) throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			ContactDTO contactDTO = null;
			for (int i = 0; i < transferJson.size(); i++) {
				JSONObject tempJson = transferJson.getJSONObject(i);
				contactDTO = JSONObject
						.toJavaObject(tempJson, ContactDTO.class);
				contactDTO.setUpdateby(userName);
				contactDTO.setCreateby(userName);
				commonDAO.insert("healthcare.addContact", contactDTO);
			}
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.INSERT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> queryInsuranceList(JSONObject transferJson) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> result2 = null;
		try {
			ContactListDTO contactListDTO = new ContactListDTO();
			contactListDTO.setQueryParams(JSONObject.toJavaObject(transferJson,
					Map.class));
			int page = Integer.parseInt(transferJson.getString("page"));
			int pageSize = Integer.parseInt(transferJson.getString("pageSize"));
			contactListDTO.setPage(page);
			contactListDTO.setPageSize(pageSize);
			contactListDTO.setDoCount(true);
			contactListDTO.setQueryAll(false);

			// result = (Map<String, Object>)
			// commonDAO.queryPage("healthcare.queryContactList",
			// contactListDTO);
			Map queryParm = contactListDTO.getQueryParams();
			// String[] contactTypes = { "1", "2" };
			List contactTypes = new ArrayList<String>();
			contactTypes.add("1");
			contactTypes.add("2");
			queryParm.put("contactType", contactTypes);
			contactListDTO.setQueryParams(queryParm);
			result2 = (Map<String, Object>) commonDAO.queryPage(
					"healthcare.queryContactList", contactListDTO);
			List contactList = (List) result2.get("result");
			for (int i = 0; i < contactList.size(); i++) {
				Map tempMap = (Map) contactList.get(i);
				String contactType = (String) tempMap.get("contactype");
				if ("1".equals(contactType)) {
					result.put("responsible", tempMap);
				} else {
					result.put("contacts", tempMap);
				}
			}
			// responsible = commonDAO.
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> updateInsruance(JSONArray transferJson,
			String username) {
		Map<String, Object> result = new HashMap<String, Object>();
		for (int i = 0; i < transferJson.size(); i++) {
			JSONObject tempJson = transferJson.getJSONObject(i);
			ContactDTO contactDTO = null;
			contactDTO = JSONObject.toJavaObject(tempJson, ContactDTO.class);
			commonDAO.update("healthcare.updateContact", contactDTO);

		}
		result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
		result.put(Constants.MSG_KEY, Constants.UPDATE_SUCCESS);
		return result;
	}
}
